package com.usian.search.controllere.v1;

import com.alibaba.fastjson.JSON;
import com.usian.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/importData")
@Slf4j
public class DB2ESController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    @PostMapping("/import")
    public String importData(@RequestBody Map<String, Object> map){
        IndexRequest indexRequest =
                new IndexRequest("app_info_article").id(map.get("id").toString()).source(map);
        try {
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("导入数据到es失败，{}", JSON.toJSONString(map));
            e.printStackTrace();
        }
        return AppHttpCodeEnum.SUCCESS.getErrorMessage();
    }




}
